//引用网络请求
import { request } from '../../request/index'
// pages/goods_detail/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsObject: {},
    isCollect: false
  },

  GoodsInfo: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function () {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1]
    let options = currentPage.options
    const { goods_id } = options;
    this.getGoodsDetail(goods_id);
  },

  //获取商品详情
  async getGoodsDetail(goods_id) {
    const res = await request({ url: '/goods/detail', data: { goods_id } })
    this.GoodsInfo = res;
    let collect = wx.setStorageSync('collect') || [];
    let isCollect = collect.some(v => v.goods_id === this.GoodsInfo.goods_id);
    this.setData({
      goodsObject: {
        goods_name: res.goods_name,
        goods_price: res.goods_price,
        goods_introduce: res.goods_introduce.replace(/\.webp/g, '.jpg'),
        pics: res.pics
      },
      isCollect
    })
  },

  //点击轮播图预览
  handlePrevewImage(e) {
    const urls = this.GoodsInfo.pics.map(v => v.pics_mid)
    console.log(e);
    const current = e.currentTarget.dataset.url;
    wx.previewImage({
      urls,
      current,
    })
  },

  //加入购物车
  handleCartAdd() {
    // console.log(111);
    // 获取缓存中我购物车的数据
    let cart = wx.getStorageSync('cart') || [];
    // 判断商品对象是否存在数组中
    let index = cart.findIndex(item => item.goods_id === this.GoodsInfo.goods_id)
    if (index === -1) {
      this.GoodsInfo.num = 1;
      this.GoodsInfo.checked = true;
      cart.push(this.GoodsInfo);
    } else {
      cart[index].num++;
    }
    wx.setStorageSync('cart', cart);
    wx.showToast({
      title: '加入成功',
      icon: 'success',
      mask: true
    })
  },

  //点击收藏
  handleCollect() {
    let isCollect = false
    let collect = wx.getStorageSync('collect') || []
    let index = collect.findIndex(item => item.goods_id === this.GoodsInfo.goods_id)
    if (index !== -1) {
      collect.splice(index, 1);
      isCollect = false;
      wx.showToast({
        title: '已取消收藏！',
        icon: 'success',
        mask: true
      })
    } else {
      collect.push(this.GoodsInfo)
      isCollect = true;
      wx.showToast({
        title: '已添加收藏！',
        icon: 'success',
        mask: true
      })
    }
    wx.setStorageSync('collect', collect)
    this.setData({
      isCollect
    })
  },

  //点击购买
  goshop(){
    console.log(this.GoodsInfo);
    let cart =wx.getStorageSync('cart')
    // console.log(cart);
    cart.push(this.GoodsInfo)
    wx.setStorageSync('cart', cart)
    wx.switchTab({
      url: '/pages/cart/index',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})